var app = getApp();

Page({
  data: {
    flag: true,
    seller: false,
    buyer: false,
    add_time: '',
    remark: '',
    reward: '',
    deposit: '',
    brokerage: '',
    attachment: [],
    lng: 0,
    lat: 0,
    province: "",
    city: ""
  },
  onLoad: function(options) {
    var that = this;
    that.setData({
      orderId: options.orderId
    })
  },
  onShow: function() {
    var that = this;
    that.getData();
    app.getLocation(function (data) {
      that.setData({
        lng: data.lng,
        lat: data.lat,
        province: data.province,
        city: data.city
      })
    });
  },
  getData: function() {
    var that = this;
    app.JsonPost("Api/Order/detail", {
      id: that.data.orderId
    }, function(res) {
      console.log(res);
      var pictures = [];
      for (var i = 0; i < res.attachment.length; i++) {
        pictures.push("https://www.saixingchi.com/" + res.attachment[i]["attachment"]);
      }
      for (var i = 0; i < res.statusHistory.length; i++) {
        console.log(res.statusHistory[i]);
        if (res.statusHistory[i]["add_time"] != "") {
          res.statusHistory[i]["add_time"] = app.formatTime(res.statusHistory[i]["add_time"], 'Y-M-D h:m');
        }
        if (res.statusHistory[i]["user_id"] == wx.getStorageSync("userId")) {
          res.statusHistory[i]["role"] = 'buy'
        } else {
          res.statusHistory[i]["role"] = 'sell'
        }
      }
      that.setData({
        add_time: app.formatTime(res.add_time, 'Y-M-D h:m'),
        order_no: res.order_no,
        order_id: res.id,
        order_state: res.state,
        order_action: res.action,
        trip_id: res.trip_id,
        trip_avatar: res.seller.avatar,
        trip_name: res.seller.user_name,
        trip_description: res.trip.description,
        trip_origin: res.trip.origin,
        trip_destination: res.trip.destination,
        trip_time: app.formatTime(res.trip.depart_date, 'Y-M-D h:m') + ' ~ ' + app.formatTime(res.trip.reach_date, 'Y-M-D h:m'),
        ticket_id: res.trip.ticket_id,
        remark: res.remark,
        reward: res.reward,
        deposit: res.deposit,
        brokerage: res.brokerage,
        attachment: pictures,
        contact: res.contact,
        buyer: res.buyer.id == wx.getStorageSync("userId") ? true : false,
        buyer_avatar: res.buyer.avatar,
        buyer_name: res.buyer.user_name,
        buyer_id: res.buyer.id,
        buyer_total: parseFloat(res.reward) + parseFloat(res.deposit) + parseFloat(res.brokerage),
        seller: res.seller.id == wx.getStorageSync("userId") ? true : false,
        seller_name: res.seller.user_name,
        seller_id: res.seller.id,
        seller_total: parseFloat(res.reward) + parseFloat(res.deposit),
        statusHistory: res.statusHistory,
        transaction_id: res.transaction_id,
        review: res.review
      });
    })
  },
  changeStatus: function(e) {
    var status = e.currentTarget.dataset.type;
    var alertTip = e.currentTarget.dataset.tip;
    var that = this;
    if (status == 3 || status == '3') {
      //确定
      that.setData({
        flag: false
      })
    } else {
      wx.showModal({
        title: '提示',
        content: alertTip,
        showCancel: true,
        success: function (res) {
          if (res.confirm) {
            var jsonData = {
              id: that.data.order_id,
              state: status,
              lng: that.data.lng,
              lat: that.data.lat,
              province: that.data.province,
              city: that.data.city
            };
            app.JsonPost("Api/Order/changeStatus", jsonData, function (res) {
              console.log(res);
              if (status == 9 || status == '9') {
                //删除
                wx.navigateBack({
                  delta: 1
                });
              } else {
                that.onShow();
              }
            })
          }
        }
      });
    }
    
  },
  commentTap: function () {
    var that = this;
    wx.navigateTo({
      url: 'toComment?orderId=' + that.data.order_id
    })
  },
  commentView: function () {
    var that = this;
    var comment = JSON.stringify(that.data.review);
    wx.navigateTo({
      url: 'comment?comment=' + comment
    })
  },
  costTap: function (e) {
    this.setData({
      cost: e.detail.value
    })
  },
  // notesTap: function (e) {
  //   this.setData({
  //     notes: e.detail.value
  //   })
  // },
  submitPop: function () {
    var that = this;
    var jsonData = {
      id: that.data.order_id,
      state: 3,
      total: that.data.cost
    };
    app.JsonPost("Api/Order/changeStatus", jsonData, function (res) {
      console.log(res);
      that.resetPop();
      that.onShow();
    })
  },
  resetPop: function () {
    this.setData({
      flag: true,
      cost: '',
      //notes: ''
    })
  },
  tripDetail: function(e) {
    var tripid = e.currentTarget.id;
    wx.navigateTo({
      url: '../trip/tripDetail?tripId=' + tripid
    })
  },
  previewImage: function(e) {
    var that = this;
    var current = e.target.dataset.src;
    var pictures = that.data.attachment;
    wx.previewImage({
      current: current,
      urls: pictures
    })
  },
  userCredit: function(e) {
    var userid = e.currentTarget.dataset.userid;
    wx.navigateTo({
      url: '../user/credit?userId=' + userid
    })
  }
})